home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / util / ftos / ftos.c next >
C/C++ Source or Header  |  1990-03-15  |  3KB  |  144 lines

  1. /********************************************************************
  2. *
  3. * FTOC V1.0                                  Salim Alam: 90.02.08
  4. *
  5. * Converts assembler output from DASM to Motorola S Record format.
  6. * The code should have been assembled using the -f2 option.  The
  7. * processor should be defined as 68HC11.
  8. *
  9. * Usage:
  10. *
  11. * ftoc <infile> [outfile]
  12. *
  13. * where "infile" is the filename of the assembler output, and
  14. * "outfile" is the name of the file to which the S record will
  15. * be output.
  16. *
  17. * If outfile is not specified, "out.s" will be used for the
  18. * output file.
  19. *
  20. *********************************************************************/
  21.  
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <libraries/dos.h>
  25. #include <libraries/dosextens.h>
  26.  
  27. main(argc,argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.  
  32.    char infilename[100],outfilename[100];
  33.    struct FileHandle *infile;
  34.    FILE *outfile;
  35.    unsigned short Org, Len, CurrAdd, ToGo;
  36.    unsigned char  CheckSum, RLen;
  37.    unsigned char buffer[100];
  38.    int blen, i;
  39.  
  40.  
  41.    /* Check args */
  42.  
  43.    if ((argc < 2) || (argc > 3)) {
  44.        printf("Usage: ftoc <infile> [outfile]\n");
  45.        exit(10);
  46.    };
  47.  
  48.    strcpy(infilename,argv[1]);
  49.    if (argc==3) 
  50.      strcpy(outfilename,argv[2]);
  51.    else
  52.      strcpy(outfilename,"out.s");
  53.  
  54.  
  55.  
  56.    /* Open files */
  57.  
  58.    if ( (infile=Open(infilename,MODE_OLDFILE)) == 0L) {
  59.       printf("Unable to open input file '%s'\n",infilename);
  60.       exit(5);
  61.    };
  62.  
  63.    if ( (outfile=fopen(outfilename,"w")) == 0L) {
  64.       printf("Unable to open output file '%s'\n",outfilename);
  65.       exit(5);
  66.    };
  67.  
  68.  
  69.  
  70.    /* Main loop */
  71.  
  72.    fprintf(outfile,"S00600004844521B\n");
  73.  
  74.    for (;;) {
  75.      blen=Read(infile,buffer,4);
  76.      if (blen != 4) break;  /* Exit if EOF */
  77.  
  78.      Org = CurrAdd = (buffer[0]+(256*buffer[1]));
  79.      Len = ToGo    = (buffer[2]+(256*buffer[3]));
  80.  
  81.      while (ToGo > 0) {  /* Process a line */
  82.         RLen = (ToGo > 16)? 16 : ToGo;
  83.         ToGo -= RLen;
  84.         blen=Read(infile,buffer,RLen);
  85.         CheckSum = RLen+3+HexAdd(CurrAdd);
  86.         fprintf(outfile,"S1");
  87.         PrintHex(outfile,(RLen + 3));
  88.         PrintHex(outfile,CurrAdd / 256);
  89.         PrintHex(outfile,CurrAdd % 256);
  90.         for (i=0; i < RLen; i++) {
  91.           PrintHex(outfile,buffer[i]);
  92.             CheckSum += buffer[i];
  93.         };
  94.         CheckSum = ~CheckSum;
  95.         PrintHex(outfile,CheckSum);
  96.         fprintf(outfile,"\n");
  97.         CurrAdd += RLen;
  98.      }; /* while */
  99.  
  100.    }; /* for */
  101.  
  102.  
  103.    /* Clean up & exit */
  104.  
  105.    fprintf(outfile,"S9030000FC\n");
  106.  
  107.    Close(infile);
  108.    fclose(outfile);
  109.  
  110. } /* main */
  111.  
  112.  
  113.  
  114. PrintHex(fp,num)
  115. FILE *fp;
  116. unsigned char num;
  117. {
  118.  
  119.   unsigned char hi,lo;
  120.   static char *hexmap[] = {'0','1','2','3','4','5','6','7',
  121.                            '8','9','A','B','C','D','E','F'};
  122.   hi = num / 16;
  123.   lo = num % 16;
  124.  
  125.   fprintf(fp,"%c%c",hexmap[hi],hexmap[lo]);
  126.  
  127. }
  128.  
  129.  
  130.  
  131. HexAdd(wnum)
  132. unsigned short wnum;
  133. {
  134.  
  135.    int hi,lo;
  136.  
  137.    hi = wnum / 256;
  138.    lo = wnum % 256;
  139.  
  140.    return(lo+hi);
  141.  
  142. }
  143.  
  144.